import requests
from lxml import etree

url = 'https://news.baidu.com/'
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.183 Safari/537.36',
    'Cookie': 'BIDUPSID=04AF705FF534F9830A6DEC13D73D12C9; PSTM=1602116049; BAIDUID=04AF705FF534F9830A513652D9FCB839:FG=1; HMACCOUNT=212F34CB2A438522; H_WISE_SIDS=148078_146000_153767_160005_150967_152055_156286_150775_159609_148867_154804_153628_159533_159382_131862_160447_150772_151018_157261_157961_127969_153144_154174_155962_159065_158527_150346_155318_146732_158746_151831_131423_128699_153149_159701_155398_159450_107316_158054_160096_154190_158520_155344_155255_159955_157790_144966_159797_159950_154213_157814_158718_158640_156848_157965_147551_158910_156710_158415_154639_159157_154257_158686_159073_110085_157006; HMACCOUNT_BFESS=212F34CB2A438522; BDORZ=FFFB88E999055A3F8A630C64834BD6D0; BDRCVFR[B3iNXbc_kLT]=mk3SLVN4HKm; H_PS_PSSID=; HMVT=e9e114d958ea263de46e080563e254c4|1604913249|'
}
r = requests.get(url=url, headers=headers)
html = etree.HTML(r.text)
comment_list = html.xpath('//strong/a[@target="_blank"]/text()')
print(comment_list)
g = open('./新闻标题.txt', 'w', encoding='utf-8')
for i in comment_list:
    print(i)
    g.write(i + '\n')
